<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * Backend PermissionsController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class Backend_PermissionsController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	/*
	 * Init class vars and stuff
	 */
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
	}
	
	
	
	/*
	 * Lists permissions
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function indexAction()
	{
		// db
		$permission	= new Permission();
		
		// query
		$getPermission	= $permission->fetchAll();
		
		
		// view
		$this->view->count		= $getPermission->count();
		$this->view->results	= $getPermission->toArray();
	}
	
	
	
	/*
	 * Add a new permission
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function addAction()
	{
		// vars
		$formData		= $this->_request->getPost();
		
		// db
		$permission 	= new Permission();
		
		// form
		$form			= new PermissionForm();
		$form->removeElement('is_deleted');
		
		if ($this->_request->isPost() && $form->isValid($formData))
		{
			$form->removeElement('submit');
			
			if($uid = $permission->insert($form->getValues()))
			{
				$this->_redirect('/backend/permission-' . $uid . '.html');
			}
		}
		else
		{
			$form->populate($formData);
		}

		$this->view->form = $form;
	}
	
	
	
	/*
	 * Edit a permission and set access rights
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function editAction()
	{
		// param
		$perm_uid	= $this->_request->getParam('uid', 0);

		// form
		$form 		= new PermissionForm();
		$formPerms	= new PermissionsForm();
		
		// db
		$permission		= new Permission();
		$permissions	= new Permissions();
		$boards			= new Boards();
		$categories		= new Categories();
		
		// query
		$queryPerm			= $permission->find($perm_uid);
		$queryCategories	= $categories->fetchAll();
		
		$getPerms 			= $permissions->select()->setIntegrityCheck(false);
		$getPerms->from(
				array('p' => 'permissions')
			)
			->joinLeft(
				array('b' => 'boards'), 'p.board_uid = b.uid', array('title')
			)
			->where('p.perm_uid = ?', $perm_uid);
			
		$queryPerms			= $permissions->fetchAll($getPerms);
		
		if($queryPerm->count() > 0)
		{
			$res		= $queryPerm->toArray();
			$formData 	= $this->_request->getPost();

			if($this->_request->isPost())
			{
				if(strlen($formData['title']) > 0 && $form->isValid($formData))
				{
					if($formData['is_deleted'] == 1 && $user_uid != 1)
					{
						$permission->delete('uid=' . $perm_uid);
						$this->_redirect('/backend/permissions.html');
					}
					
					$form->removeElement('is_deleted');
					$form->removeElement('submit');
					$permission->update($form->getValues(), 'uid=' . $perm_uid);
				}
				elseif(isset($formData['board_uid']))
				{
					$permissions->insert(array(
						'perm_uid'		=> $perm_uid,
						'board_uid'		=> $formData['board_uid'],
						'has_access'	=> $formData['has_access'],
						'is_moderator'	=> $formData['is_moderator']
					));
				}
				
				$this->_redirect('/backend/permission-' . $perm_uid . '.html');
			}
			else
			{
				$getBoard 	= $formPerms->getElement('board_uid');
				$uid		= array();
				
				if($queryCategories->count() > 0)
				{
					foreach($queryCategories->toArray() AS $result)
					{
						
						$getBoard->addMultiOption('cat_' . $result['uid'], $result['title']);
						$uid[] = 'cat_' . $result['uid'];
						$queryBoards = $boards->fetchAll('category_uid = ' . $result['uid']);
						
						if($queryBoards->count() > 0)
						{
							foreach($queryBoards->toArray() AS $bResult)
							{
								$getBoard->addMultiOption($bResult['uid'], '- ' . $bResult['title']);
							}
						}
					}
					
					$getBoard->setAttrib('disable', $uid);
				}
				
				$form->populate($res[0]);
			}
		}
		else
		{
			$this->_redirect('/backend/permissions.html');
		}

		// view
		$this->view->form 			= $form;
		$this->view->formPerm 		= $formPerms;
		
		$this->view->perms_count	= $queryPerms->count();
		$this->view->perms_res		= $queryPerms->toArray();
	}
	
	
	
	/*
	 * Toggle Access
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function setaccessAction()
	{
		// params
		$uid = $this->_request->getParam('uid', 0);
		
		// db
		$permissions 	= new Permissions();
		
		// query
		$getPerms		= $permissions->find($uid);

		if($getPerms->count() > 0)
		{
			$resPerms 	= $getPerms->toArray();
			$access		= $resPerms[0]['has_access'] == 0 ? 1 : 0;
			$perm_uid	= $resPerms[0]['perm_uid'];
			
			$permissions->update(array('has_access' => $access), 'uid=' . $uid);
			$this->_redirect('/backend/permission-' . $perm_uid . '.html');
		}
		else
		{
			$this->_redirect('/backend/permissions.html');
		}
	}
	
	
	
	/*
	 * Toggle IsMod-Perm
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function setmodAction()
	{
		// params
		$uid = $this->_request->getParam('uid', 0);
		
		// db
		$permissions 	= new Permissions();
		
		// query
		$getPerms		= $permissions->find($uid);

		if($getPerms->count() > 0)
		{
			$resPerms 	= $getPerms->toArray();
			$mod		= $resPerms[0]['is_moderator'] == 0 ? 1 : 0;
			$perm_uid	= $resPerms[0]['perm_uid'];
			
			$permissions->update(array('is_moderator' => $mod), 'uid=' . $uid);
			$this->_redirect('/backend/permission-' . $perm_uid . '.html');
		}
		else
		{
			$this->_redirect('/backend/permissions.html');
		}
	}
	
	
	
	/*
	 * Delete a permission
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function deleteAction()
	{
		// params
		$uid = $this->_request->getParam('uid', 0);
		
		// db
		$permissions 	= new Permissions();
		
		// query
		$getPerms		= $permissions->find($uid);
		
		if($getPerms->count() > 0)
		{
			$resPerms	= $getPerms->toArray();
			$perm_uid	= $resPerms[0]['perm_uid'];
			
			$permissions->delete('uid='. $uid);
			$this->_redirect('/backend/permission-' . $perm_uid . '.html');
		}
		else
		{
			$this->_redirect('/backend/permissions.html');
		}
	}
}
?>